package com.zaofan.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zaofan.bean.OpenCity;
import com.zaofan.dao.OpenCityDao;

public class OpenCityService {
	private OpenCityDao openCityDao = new OpenCityDao();

	public void delete(OpenCity openCity) {
		openCityDao.del(openCity);
	}

	public void add(OpenCity openCity) {
		openCity.setAddTime(new Date());
		openCity.setAreaNum(0);
		openCity.setBuilderNum(0);
		openCity.setShopNum(0);
		openCityDao.add(openCity);
	}

	public void update(OpenCity openCity) {
		openCityDao.update(openCity);
		OpenBuilderService builderService = new OpenBuilderService();
		OpenAreaService areaService = new OpenAreaService();
		areaService.updateCityName(openCity);
		builderService.updateCityName(openCity);
	}

	public void updateState(OpenCity openCity) {
		openCityDao.update(openCity);
		OpenBuilderService builderService = new OpenBuilderService();
		OpenAreaService areaService = new OpenAreaService();
		areaService.updateCityName(openCity);
		builderService.updateCityName(openCity);
		if(openCity.getState() == 0){
			builderService.updateStateUnShowByCityId(openCity.getId());
			areaService.updateStateUnShowByCityId(openCity.getId());
		}
	}

	public List<OpenCity> findAll(Map<String, Object> params) {
		List<OpenCity> data = openCityDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return openCityDao.count(params);
	}

	public OpenCity findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return openCityDao.findOne(params);
	}

	public OpenCity findByCityName(String cityName) {
		Map<String, Object> params = new HashMap<>();
		params.put("cityName", cityName);
		return openCityDao.findOne(params);
	}

}
